/*
 * GAdminConsole.java
 * Created on 29 sept. 2008
 *
 * Glacéo Internet Platform
 * http://sourceforge.net/projects/chm/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hockeo.vhadmin;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.TreeMap;

import com.hockeo.vhbase.error.GException;
import com.hockeo.vhbase.utils.misc.GConsoleApp;

/**
 * Command-line utility that allows controlling a hockey season without requiring a web
 * and application server.
 *
 * @version $Id$
 * @author jjanke
 */
public class GAdminConsole extends GConsoleApp
{
  private TreeMap<String, IGAdminCommand> d_mapCommands;

  /**
   * Instantiates a new administration console.
   */
  private GAdminConsole()
  {
    super( "classpath:com/hockeo/vhadmin/spring-context.xml" );

    BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
    String strCmd = null;
    String[] astrCmdLine = null;

    registerCommands();

    System.out.println( "Please type 'h' for an overview of all available commands." );

    do
    {
      System.out.print( "VirtualHockey $ " );

      try
      {
        astrCmdLine = br.readLine().split( "\\s" );
        strCmd = astrCmdLine[0];
      }
      catch ( IOException ex )
      {
        ex.printStackTrace();
        strCmd = "q";
      }

      if ( strCmd.equals( "h" ) )
        showHelp();
      else if ( strCmd.equals( "q" ) )
        quit();
      else
      {
        IGAdminCommand adminCommand = d_mapCommands.get( strCmd );

        if ( adminCommand != null )
        {
          try
          {
            long lStartTime = System.currentTimeMillis();
            adminCommand.execute( astrCmdLine );
            System.out.println( "Command executed in " + ( ( System.currentTimeMillis() - lStartTime ) / 1000.0 )
                + " seconds." );
          }
          catch ( GException ex )
          {
            ex.printStackTrace();
          }
        }
        else
          System.out.println( "Unknown command. Use 'h' for an overview of all available commands." );
      }
    }
    while ( true );
  }

  /**
   * Registers all available commands.
   */
  @SuppressWarnings( "unchecked" )
  private void registerCommands()
  {
    d_mapCommands = (TreeMap<String, IGAdminCommand>) getBean( "adminCommandMap" );
  }

  /**
   * Shows a list of all available commands.
   */
  private void showHelp()
  {
    System.out.println( "" );
    System.out.println( "--- Command Overview ---" );
    System.out.println( "========================\n" );

    System.out.println( "> 1 - System commands:" );
    System.out.println( "      h = Help (shows this overview)" );
    System.out.println( "      q = Quit (quits the administration console)" );
    System.out.println( "" );

    System.out.println( "> 2 - Administration commands:" );

    for ( String strCmdName : d_mapCommands.keySet() )
    {
      IGAdminCommand cmd = d_mapCommands.get( strCmdName );
      System.out.println( "      " + cmd.getShortCut() + " = " + cmd.getName() + " (" + cmd.getShortDesc() + ")" );
    }

    System.out.println( "" );
  }

  private void quit()
  {
    System.out.println( "Good bye!" );
    System.exit( 0 );
  }

  /**
   * Main method to start the admin console.
   */
  public static void main( String[] args )
  {
    new GAdminConsole();
    System.exit( 0 );
  }
}
